﻿Imports System.Data.SqlClient
Public Class DistritoDAO
    Public Function insertarDistrito(ByVal modelo As Distrito) As Distrito
        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "INSERT INTO Distrito (  CABINA_IdCabina, Nombre_Distrito ) VALUES (@VALOR1, @VALOR2)"

            Dim cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@VALOR1", modelo.IdCabina)
            cmd.Parameters.AddWithValue("@VALOR2", modelo.Nombre_distrito)

            Dim afectados = cmd.ExecuteNonQuery

            If afectados <> 1 Then
                Throw New DAOExcepcion("DistritoDAO: No insertó")
            End If

            ' LEER EL ULTIMO ID
            sql = "SELECT @@IDENTITY"
            cmd = New SqlCommand(sql, cn)
            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()
            If dr.Read Then
                modelo.IdDistrito = CType(dr(0), String)
            End If

            dr.Close()
            cn.Close()

        Catch ex As Exception
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return modelo
    End Function

End Class

